﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Syngine.Components.ParticleSystems
{
	/// <summary>
	/// 
	/// </summary>
	public class ParticleEmitter : Updatable
	{
		private readonly Random _random = new Random(DateTime.Now.Millisecond);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="region"></param>
		/// <param name="minAngle"></param>
		/// <param name="maxAngle"></param>
		public ParticleEmitter(Rectangle region, Vector2 minAngle, Vector2 maxAngle)
		{
			Region = region;
			MinAngle = minAngle;
			MaxAngle = maxAngle;
		}

		/// <summary>
		/// 
		/// </summary>
		public Vector2 MinAngle { get; set; }

		/// <summary>
		/// 
		/// </summary>
		public Vector2 MaxAngle { get; set; }

		/// <summary>
		/// 
		/// </summary>
		protected Rectangle Region { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public virtual void Emit(Particle particle)
		{
			particle.Position = new Vector2(_random.Next(Region.X, Region.X + Region.Width), _random.Next(Region.Y, Region.Y + Region.Height));
			particle.Direction = Vector2.Normalize(new Vector2(_random.Next((int)MinAngle.X, (int)MaxAngle.X), _random.Next((int)MinAngle.Y, (int)MaxAngle.Y)));
			particle.LoadContent(GameContext.CreateLoadContext());
		}
		
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public virtual void Emit(IEnumerable<Particle> particles)
		{
			foreach (var particle in particles)
			{
				Emit(particle);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		protected override void UpdateCore(UpdateContext context)
		{
		}
	}
}